/*
 * Copyright (c) 2011. c.mueller
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * The Software shall be used for Good, not Evil.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package de.altitude.cimuelle.db;

import de.altitudecustomcommands.Player;
import org.apache.log4j.Logger;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.Date;

/**
 * User: cybernaut
 * Date: 19.08.11
 * Time: 00:12
 */
public class DatabasePlayerInfo {

    private int rank;
    private String vaporId;
    private String name;
    private int turretKills;
    private Date lastReset;
    private Date lastUpdated;
    private String port;
    private int kills;
    private int deaths;
    private int goals;
    private int coopPoints;

    private final static Logger LOGGER = Logger.getLogger("DatabasePlayerInfo");

    public DatabasePlayerInfo(final String port, final String vaporId, final String name, final int turretKills, final Date lastReset, final Date lastUpdated) {
        this.port = port;
        this.vaporId = vaporId;
        this.name = name;
        this.turretKills = turretKills;
        this.lastReset = lastReset;
        this.lastUpdated = lastUpdated;
    }

    public DatabasePlayerInfo(final String port, final int counter, final String vaporId, final String name, final int turretKills, final Timestamp lastReset, final Timestamp lastUpdated) {
        this.port = port;
        this.rank = counter;
        this.vaporId = vaporId;
        this.name = name;
        this.turretKills = turretKills;
        this.lastReset = lastReset;
        this.lastUpdated = lastUpdated;
    }

    public DatabasePlayerInfo(final String port, final Player player, final int kills, final int deaths, final int goals) {
        this.vaporId = player.getVapor();
        this.name = player.getName();
        this.port = port;
        this.kills = kills;
        this.deaths = deaths;
        this.goals = goals;
    }

    public DatabasePlayerInfo(final String port, final int counter, final String vaporId, final String name, final int kills, final int deaths, final int goals) {
        this.port = port;
        this.rank = counter;
        this.vaporId = vaporId;
        this.name = name;
        this.goals = goals;
        this.kills = kills;
        this.deaths = deaths;
    }

    public DatabasePlayerInfo(final int counter, final ResultSet resultSet) throws SQLException {
        this.port = resultSet.getString("port");
        this.rank = counter;
        this.vaporId = resultSet.getString("vaporId");
        this.name = resultSet.getString("name");
        this.turretKills = resultSet.getInt("turretKills");
        this.kills = resultSet.getInt("kills");
        this.deaths = resultSet.getInt("deaths");
        this.goals = resultSet.getInt("goals");
        this.lastReset = resultSet.getTimestamp("lastResetTimestamp");
        this.lastUpdated = resultSet.getTimestamp("lastUpdated");
        this.coopPoints = resultSet.getInt(DatabaseManager.COOP_POINTS_COL);
    }

    public String getPort() {
        return port;
    }

    public void setPort(final String port) {
        this.port = port;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(final int rank) {
        this.rank = rank;
    }

    public String getVaporId() {
        return vaporId;
    }

    public void setVaporId(final String vaporId) {
        this.vaporId = vaporId;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public int getTurretKills() {
        return turretKills;
    }

    public void setTurretKills(final int turretKills) {
        this.turretKills = turretKills;
    }

    public Date getLastReset() {
        return lastReset;
    }

    public void setLastReset(final Date lastReset) {
        this.lastReset = lastReset;
    }

    public Date getLastUpdated() {
        return lastUpdated;
    }

    public void setLastUpdated(final Date lastUpdated) {
        this.lastUpdated = lastUpdated;
    }

    public int getGoals() {
        return goals;
    }

    public int getKills() {
        return kills;
    }

    public int getDeaths() {
        return deaths;
    }

    public void setKills(final int kills) {
        this.kills = kills;
    }

    public void setDeaths(final int deaths) {
        this.deaths = deaths;
    }

    public void setGoals(final int goals) {
        this.goals = goals;
    }

    public String getRatio() {
        String ratioS;
        if (getDeaths() == 0) {
            ratioS = "n.n.";
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("#0.00");
            final double ratio = (double) getKills() / (double) getDeaths();
            ratioS = decimalFormat.format(ratio);
        }
        return ratioS;
    }

    public int getCoopPoints() {
        return coopPoints;
    }

    public void setCoopPoints(int coopPoints) {
        this.coopPoints = coopPoints;
    }

    public boolean saveToDB() {
        synchronized (this) {
            return DatabaseManager.getInstance().updatePlayerInfo(this);
        }
    }
}
